package com.jk.controller;

import com.alibaba.druid.sql.ast.statement.SQLForeignKeyImpl;
import com.jk.po.PersonalPO;
import com.jk.service.PersonalService;
import com.jk.utils.OssClientUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version : V1.0
 * @ClassName: Personalcontroller
 * @Description:描述一下类的作用
 * @Auther: 郑滨
 * @Date: 2021/6/30 9:50
 */
@RestController
@RequestMapping("personal")
@Api("个人中心展示")
public class PersonalController {

    //日志记录对象
    private static Logger logger = LoggerFactory.getLogger(TestController.class);

    //注入Service层
    @Autowired
    private PersonalService personalService;
    //注入oss工具包
    @Autowired
    private OssClientUtil ossClientUtil;

    /**
    * @Author  郑滨
    * @method  queryPersonal
    * @description 查询个人中心
    * @Date 9:57 2021/6/30
    * @Param  * @param
    * @return java.util.Map<java.lang.String,java.lang.Object>
    **/
    @PostMapping ("queryPersonalById/{id}")
    @ApiOperation("登录之后根据登录ID查询个人中心数据")
    public PersonalPO queryPersonalById(@PathVariable("id") @ApiParam("根据id回显个人信息") Integer id){
        PersonalPO personalPO = new PersonalPO();
        //personalPO.setId(id);
        try {
            personalPO= personalService.queryPersonalById(id);
            logger.info("查询数据成功");
        }catch (Exception e){
            logger.error("查询个人中心数据异常"+e.getMessage());
        }
        return personalPO;
    }
    /**
    * @Author  郑滨
    * @method  updatePersonalPassWord
    * @description 描述一下方法的作用
    * @Date 16:09 2021/6/30
    * @Param  * @param
    * @return void
    **/
    @PostMapping("updatePersonalPassWord")
    @ApiOperation(value = "修改客户密码")
    public Map<String,Object> updatePersonalPassWord(@RequestParam("cuspassword") String cuspassword,@RequestParam("id") Integer id){
        Map<String, Object> map = new HashMap<>();
        try {
            personalService.updatePersonalPassWord(cuspassword,id);
            map.put("code","200");
            map.put("message","修改密码成功");
            logger.info("修改密码成功");
        }catch (Exception e){
            map.put("code","400");
            map.put("message","修改密码异常");
            logger.info("修改密码失败");
        }
        return map;
    }
    /**
    * @Author  郑滨
    * @method  updatePersonalImg
    * @description 描述一下方法的作用
    * @Date 16:01 2021/7/1
    * @Param  * @param img
    * @return java.lang.String
    **/
    @PostMapping("updatePersonalImg")
    @ApiOperation("修改上传头像")
    public String updatePersonalImg(@ApiParam("上传的图片") MultipartFile img){
        String url = "";
        try {
            String s = ossClientUtil.uploadImg2Oss(img);
            url = ossClientUtil.getImgUrl(s);
            logger.info("上传图片成功");
        }catch (Exception e){
            logger.info("上传图片失败");
        }
        return url;
    }
}
